package cn.terminus.service.customer.job;

import cn.terminus.biz.customer.contractinfo.ContractInfoManager;
import cn.terminus.biz.customer.custompurposeroom.CustomPurposeRoomManager;
import cn.terminus.common.customer.bean.contractinfo.dto.ContractInfoQueryDto;
import cn.terminus.common.customer.bean.contractinfo.dto.ContractInfoResultDto;
import cn.terminus.common.customer.bean.custompurposeroom.dto.CustomPurposeRoomResultDto;
import cn.terminus.common.customer.bean.custompurposeroom.dto.CustomRoomInfoGetDto;
import cn.terminus.common.customer.constant.ContractStatusEnum;
import cn.terminus.common.customer.constant.CustomPurposeRoomTypeEnum;
import cn.terminus.result.common.exception.DataAccessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author leslie.zhang
 * @date 2021/9/16 2:16 下午
 */
@Component
@Slf4j
public class ContractRoomEffectiveStatusTask {
    @Resource
    ContractInfoManager contractInfoManager;

    @Resource
    CustomPurposeRoomManager customPurposeRoomManager;

    @Resource
    RoomStatusHandler roomStatusHandler;

    // 查询今天起执行的合同，把其对应的房间状态修改
    @Scheduled(cron = "0 30 0 * * ?")
    public void execute() {
        LocalDate now = LocalDate.now();
        ContractInfoQueryDto queryDto = new ContractInfoQueryDto();
        queryDto.setEffectiveBeginTime(now);
        queryDto.setStatus(ContractStatusEnum.OPEN.getCode());
        try {
            // 查找今天开始的所有有效合同，修改对应的房间状态
            List<ContractInfoResultDto> resultDtoList = contractInfoManager.query(queryDto);
            if (!CollectionUtils.isEmpty(resultDtoList)) {
                List<Long> contractIds = resultDtoList
                        .stream()
                        .map(ContractInfoResultDto::getId)
                        .collect(Collectors.toList());
                if (contractIds.size() > 0) {
                    // 合同 id->type  对应关系
                    Map<Long, Integer> contractTypeMap = resultDtoList
                            .stream()
                            .collect(Collectors.toMap(ContractInfoResultDto::getId, ContractInfoResultDto::getType));
                    CustomRoomInfoGetDto roomInfoGetDto = new CustomRoomInfoGetDto();
                    roomInfoGetDto.setContractIds(contractIds);
                    roomInfoGetDto.setType(CustomPurposeRoomTypeEnum.CONTRACT.getCode());
                    List<CustomPurposeRoomResultDto> roomResultDtos = customPurposeRoomManager
                            .queryByContractIdsAndType(roomInfoGetDto);
                    if (!CollectionUtils.isEmpty(roomResultDtos)) {
                        roomResultDtos.forEach(r -> {
                            Long contractId = r.getContractId();
                            Long cboRoomId = r.getCboRoomId();
                            Integer type = contractTypeMap.get(contractId);
                            roomStatusHandler.sendMsgWithEffective(type, cboRoomId);
                        });
                    }
                }
            }
        } catch (DataAccessException e) {
            e.printStackTrace();

        }

    }

}
